<?php

namespace App\Http\Service;

use App\Util\Result;
use App\Model\Region;
use Illuminate\Support\Facades\DB;

trait ServiceRegion{

    /**
     * czs
     * @return mixed
     * 景区地区分类列表
     */
    public static function getList($keyWord,$limit){
        if (empty($keyWord)) {
            $list = DB::table('regions as a')
                ->leftjoin('regions as b','a.pid','=','b.id')
                ->select('a.name','b.name as pname','a.status','a.id','a.created_at')
                ->paginate($limit);
        } else {
            $list = DB::table('regions as a')
                ->leftjoin('regions as b','a.pid','=','b.id')
                ->select('a.name','b.name as pname','a.status','a.id','a.created_at')
                ->where("name","like", '%'.$keyWord.'%')
                ->paginate($limit);
        }
        return Result::success()->setData($list);
    }

    /**
     *获取单个id记录
     * czs
     */
    public static function getOne($id) {
        $res = Region::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     *更新记录
     * czs
     */
    public static function updateData($input, $id) {
        $res = Region::where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    /**
     * czs
     *删除记录
     */
    public static function deleteLine($id) {
        $res = Region::where("id", $id)->orWhere('pid',$id)->delete();
        return Result::success()->setData($res);
    }

    /**
     * 禁用、启用
     * czs
     */
    public static function able($id,$able) {
        $sel = Scenery::where('region_id',$id)->count();
        if (!empty($sel)) {
            return Result::failure()->setMsg("该类型下面存在景区，请删除后再禁用！");
        }
        Region::where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("启用")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     * 父类别下拉
     * czs
     */
    public static function parentList() {
        $res = Region::where('pid',0)
            ->select('id', 'name')->get();
        return Result::success()->setData($res);
    }
    
    public static function getByPidName($pid, $name) {
    	$res = Region::where(array('pid'=>$pid,'name'=>$name))->select('id')->get();
    	return Result::success()->setData($res);
    }
    
}


